Uec3.ShowPartWindow = Ext.extend(Ext.Window, {
	initPanel : function() {
		
		// 来源下拉框
		var sourceType_store = getDictionaryStore(RES_PATH, {
			group : 'part_source'
		});
		this.source_cmb = getDictionaryComboBox(sourceType_store, 'local', 'part_source',
				'来源', 'source');
		sourceType_store.load();
		
		 var ds_measurementUnit_select = new Ext.data.JsonStore({
					url : Uec3.App.Constants.homeUrl + "/part/unit_getUnits.do",					
					fields : [{name : 'name'}]
		}); 
		ds_measurementUnit_select.load();
		this.unit_cmb=getUnitComboBox(ds_measurementUnit_select,false, 'local', 'measurementUnit','单位', 'measurementUnit');
		
		this.firstCategoryStore=getFirstCategoryStore(Uec3.App.Constants.homeUrl);
		this.firstCategoryStore.load();
		this.firstCategoryCmb=getFirstCategroyCombox(this.firstCategoryStore,false);
		
		this.secondCategoryStore=getSecondCategoryStore(Uec3.App.Constants.homeUrl);
		this.secondCategoryCmb=getSecondCategroyCombox(this.secondCategoryStore,false);
		
		this.firstCategoryCmb.on('select', function(combo) {
            this.secondCategoryCmb.clearValue();
			this.secondCategoryStore.removeAll();
			this.secondCategoryStore.setBaseParam('level1Id',combo.getValue());
			this.secondCategoryStore.load();
		 }.createDelegate(this));
		
		
		this.advancedQueryPanel = this.createAdvancedQueryPanel();
		this.openWindow = this.createOpenWindow();
		this.editPanel=this.createEditPanel();
        this.editWindow=this.createEditWindow();
       
		this.record = this.createRecord();
		this.colName = this.createColName();
		this.store = this.createStore();
		this.grid = this.createGrid();

		this.joinPanel = this.createJoinPanel();

		this.add(this.joinPanel);
		this.doLayout();
		this.show();
		this.creator.app.getDesktop().addAutoRun(this.getId());
		this.store.load();
		
	},
	createJoinPanel : function() {
		return new Ext.FormPanel({
					items : [{
						height : 60,
						layout : 'hbox',
						border : false,
						layoutConfig : {
							padding : '5',
							align : 'middle'
						},
						items : [{
									xtype : 'label',
									text : '浏览零件信息',
									style : 'color:#000;font-size:26px;background:#fff'
								}, {
									xtype : 'spacer',
									flex : 1
								}, {
									xtype : 'button',
									text : '高级查找',
									handler : this.queryByComplex
											.createDelegate(this)
								}]
					}, this.grid]
				});
	},

	queryByComplex : function() {
		this.advancedQueryPanel.getForm().reset();
		this.openWindow.show();
	},
	createAdvancedQueryPanel : function() {
		return new Ext.form.FormPanel({
					labelSeparator : "：",
					frame : true,
					border : false,
					items : [{
								xtype : 'textfield',
								width : 200,
								fieldLabel : '编    码',
								name : 'code'
							}, {
								xtype : 'textfield',
								width : 200,
								fieldLabel : '拼音码',
								name : 'pyCode'
							}, {
								xtype : 'textfield',
								width : 200,
								fieldLabel : '名    称',
								name : 'name'
							}, {
								xtype : 'textfield',
								width : 200,
								fieldLabel : '一级分类名称',
								name : 'level1Name'
							}, {
								xtype : 'textfield',
								width : 200,
								fieldLabel : '二级分类名称',
								name : 'level2Name'
							}],
					buttons : [{
								text : '查找',
								handler : this.searchResultByCondition.createDelegate(this)
							}, {
								text : '关闭',
								handler : function() {
									this.openWindow.hide();
								}.createDelegate(this)
							}]

				});
	},
	
	gridEdit : function() {
		var record = this.grid.getSelectionModel().getSelected();
		if(record){
		  this.editPanel.getForm().reset();
		  this.editWindow.show();
		  this.secondCategoryStore.setBaseParam('level1Id',record.get('level1Id'));
		  this.secondCategoryStore.load({callback:function(){
		   this.editPanel.getForm().loadRecord(record);
		  }.createDelegate(this)});
		 
		  
		  
//		   Ext.Ajax.request({
//				     url:Uec3.App.Constants.homeUrl + "/part/getSubCategoryListById.do?id="+record.get('partCategoryId'),
//				     waitMsg : '正在提载入数据...',
//				     success:function(response,options){
//	                  var result = Ext.decode(response.responseText);
//				      this.secondCategoryStore.loadData(result);
//					  
//	           		}.createDelegate(this),
//	           		failure:function(response,options){
//	                	Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
//	           		}
//				 });
		  
		  
		}else{
			
			Ext.MessageBox.alert("提示","请选择一条记录");
		}
		
	},
	createEditPanel : function() {
		return new Ext.form.FormPanel({
					labelSeparator : "：",
					frame : true,
					border : false,
					items : [{
								xtype:'hidden',
								name:'id'
								},{
								xtype : 'textfield',
								width : 200,
								fieldLabel : '编    码',
								name : 'code',
								disabled:true,
								allowBlank : false
							},{
								xtype : 'textfield',
								width : 200,
								fieldLabel : '名    称',
								name : 'name',
								allowBlank : false
							},{
								xtype : 'textfield',
								fieldLabel : '英文名称(*)',
								allowBlank : true,
								name : 'nameEn'
							},{
								xtype : 'textfield',
								width : 200,
								fieldLabel : '拼音码',
								name : 'pyCode',
								disabled:true
							}, this.unit_cmb,this.firstCategoryCmb,this.secondCategoryCmb],
					buttons : [{
								text : '保存',
								handler:this.updatePart.createDelegate(this)
							}, {
								text : '关闭',
								handler:function() {
									this.editWindow.hide();
								}.createDelegate(this)
							}]

				});
	},
	createEditWindow : function() {
		return new Ext.Window({
					title : '编辑零件属性',
					layout : 'fit',
					width : 380,
					closeAction : 'hide',
					height : 330,
					resizable : true,
					shadow : true,
					modal : true,
					closable : true,
					bodyStyle : 'padding:5 5 5 5',
					animCollapse : true,
					items : [this.editPanel]
				});
	},
	updatePart:function(){
		
		var frm =this.editPanel.getForm();
		
		
		if(!frm.isValid()){
			return false;
		}

		var json=frm.getValues();
		json.level1Name=this.firstCategoryCmb.getRawValue();
		json.level2Name=this.secondCategoryCmb.getRawValue();
		Ext.Ajax.request({
					waitMsg:'请稍后.....',
					waitTitle:'提示',
					url:Uec3.App.Constants.homeUrl+'/part/part_doModi.do',
			        method :"post",	
			        params : {
						queryParam : Ext.util.JSON.encode(json)
				    },
			        success:function(response,options){
			        	var result = Ext.decode(response.responseText);
			        	if(result.success){
			        		//this.store.reload();
			        		Ext.Msg.alert("信息",result.msg);
                            //this.editWindow.hide();
			        	}else{
			        		
			        		Ext.Msg.alert("信息",result.msg);
			        	}
			        },
			      failure:function(form,action){
			          Ext.Msg.alert('提示','操作失败  ');
			      }
			});
	},
	searchResultByCondition : function() {
		var form_values = this.advancedQueryPanel.getForm().getValues();
	
		for (var j in form_values) {
			form_values[j] = castNoSelToNULL(form_values[j]);
		}
			
		form_values = Ext.util.JSON.encode(form_values);
		if(form_values) {
			this.store.baseParams = {'queryParam' : form_values};
		} else {
			this.store.baseParams = {};
		}
		this.store.load();
		this.openWindow.hide();
	},
	createOpenWindow : function() {
		return new Ext.Window({
					title : '高级查找',
					layout : 'fit',
					width : 380,
					closeAction : 'hide',
					height : 330,
					resizable : true,
					shadow : true,
					modal : true,
					closable : true,
					bodyStyle : 'padding:5 5 5 5',
					animCollapse : true,
					items : [this.advancedQueryPanel]
				});
	},
	createRecord : function() {
		return Ext.data.Record.create([{
					name : 'id'
				}, {
					name : 'code'
				}, {
					name : 'source'
				}, {
					name : 'name'
				}, {
					name : 'nameEn'
				}, {
					name : 'pyCode'
				}, {
					name : 'measurementUnit'
				},{
					name : 'level1Id'
				}, {
					name : 'level1Name'
				}, {
					name : 'level2Name'
				}, {
					name : 'durability'
				},{
				    name : 'partCategoryId'
				}]);
	},
	createColName : function() {

		return new Ext.grid.ColumnModel([
				{
					header : '编    码',
					dataIndex : 'code'
				}, {
					header : '来    源',
					dataIndex : 'source',
					renderer : Ext.util.Format.comboRenderer(this.source_cmb)
				}, {
					header : '名    称',
					dataIndex : 'name'
				},{
					header : '拼音码',
					dataIndex : 'pyCode'
				}, {
					header : '英文名称',
					dataIndex : 'nameEn'
				}, {
					header : '计量单位',
					dataIndex : 'measurementUnit'
				}, {
					header : '一级分类名称',
					dataIndex : 'level1Name'
				}, {
					header : '二级分类名称',
					dataIndex : 'level2Name'
				}, {
					header : '耐用性',
					dataIndex : 'durability'
				},{
					header : '',
					dataIndex : 'level1Id',
					hidden :true
				},{
					header : '',
					dataIndex : 'partCategoryId',
					hidden :true
				}]);
	},
	createStore : function() {
		return new Ext.data.JsonStore({
					url : RES_PATH + '/part/listPart.do',
					mode : "local",
					totalProperty : "totalProperty",
					root : "root",
					idProperty : "id",
					pruneModifiedRecords : true,
					fields : this.record
				});
	},
	createGrid : function() {
		var toolbar=this.createToolbar();
		return new Ext.grid.GridPanel({
					title : '数据明细',
					cm : this.colName,
					store : this.store,
					iconCls : 'icon-grid',
					loadMask : {
						msg : '数据加载中...'
					},
					viewConfig : {
						forceFit : true
					},
					height : 380,
					tbar:toolbar,
					bbar : new Ext.PagingToolbar({
								pageSize : Uec3PageSize,
								store : this.store,
								displayInfo : true,
								displayMsg : '第 {0} - {1} 条  共 {2} 条',
								emptyMsg : "没有记录"

							})
				});
	},
	createToolbar:function(){
		
		 return  new Ext.Toolbar({
		     items:[
			    {text : '编辑零件',iconCls : 'icon-edit',handler:this.gridEdit.createDelegate(this)}			    	 
		     ]
		  });
	}

});


Uec3.ShowPartWindowCreator = Ext.extend(Ext.app.Module, {
			id : 'showPartWindowCreator',
			type : 'common/notepad',

			createWindow : function() {
				var desktop = this.app.getDesktop();
				var win = desktop.getWindow('showPartWindow');
				if (win) {
					win.toFront();
				} else {
					win = desktop.createWindow({
								id : 'showPartWindow',
								title : '浏览零件信息',
								width : 1010,
								height : 500,
								iconCls : 'acc-icon',
								animCollapse : true,
								constrainHeader : true,
								maximizable : true,
								taskbuttonTooltip : "浏览零件信息",
								layout : "fit",
								items : []
							}, Uec3.ShowPartWindow);

					win.creator = this;
					win.initPanel();
				}
			}
		});